<template>
    <el-card class="card f fv">
        <div class="f fac fpj">
            <div class="title">供应商列表</div>
            <div class="operation">
                <el-button type="primary" @click="openDialog(null, '添加')">添加供应商</el-button>
            </div>
        </div>
        <!-- 搜索条件 -->
        <el-form style="margin-top: 0.12rem;" :model="data.searchform" :inline="true">
            <el-form-item label="供应商名称">
                <el-input clearable style="width: 2rem;" @clear="getList(true)" v-model="data.searchform.name" placeholder="请输入供应商名称" />
            </el-form-item>
            <el-form-item label="联系电话">
                <el-input clearable style="width: 2rem;" @clear="getList(true)" v-model="data.searchform.contact_phone" placeholder="请输入联系电话" />
            </el-form-item>
            <el-form-item label="地址">
                <el-input clearable style="width: 2rem;" @clear="getList(true)" v-model="data.searchform.address" placeholder="请输入地址" />
            </el-form-item>
            <el-form-item label="状态">
                <el-select style="width: 2rem;" clearable @change="getList(true)" v-model="data.searchform.isDue"
                    placeholder="请选择状态">
                    <el-option label="合作中" :value="1" />   
                    <el-option label="已到期" :value="0" />                               
                </el-select>
            </el-form-item>
            <el-form-item label="">
                <el-button type="primary" @click="getList(true)">搜索</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table 
            :data="data.tableList" 
            :height="data.tableHeight" 
            v-loading="data.tableLoading" 
            stripe
            class="table">
            <el-table-column prop="name" align="center" label="供应商" />              
            <el-table-column prop="contact_person" align="center" label="联系人" />
            <el-table-column prop="contact_phone" align="center" label="联系电话" />
            <el-table-column prop="address" align="center" label="地址" />
            <el-table-column prop="contact_email" align="center" label="电子邮箱" />
            <el-table-column prop="" align="center" label="合作日期" width="200">
                <template #default="scope">
                    {{ scope.row.start_time && utils.formatDate(new Date(scope.row.start_time).getTime(), 'Y-M-D') }}——{{ scope.row.end_time && utils.formatDate(new Date(scope.row.end_time).getTime(), 'Y-M-D') }}
                </template>
            </el-table-column>
            <el-table-column prop="status" align="center" label="状态">
                <template #default="scope">                    
                    <el-tag type="success" v-if="new Date(scope.row.end_time).getTime() > new Date().getTime()">合作中</el-tag>
                    <el-tag type="danger" v-else>已到期</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="status" align="center" label="创建时间">
                <template #default="scope">
                    <span>{{ scope.row.create_time && utils.formatDate(new Date(scope.row.create_time).getTime(), 'Y-M-D H:I:S') }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="操作">
                <template #default="scope">
                    <span class="table-edit" @click="openDialog(scope.row, '编辑')">编辑</span>
                    <span class="table-delete" @click="deleteSupplier(scope.row)">删除</span>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination background style="margin-top: 0.16rem;" layout="->, total, sizes, prev, pager, next" :pager-count="5"
            v-model:current-page="data.pageNum" v-model:page-size="data.pageSize"
            :total="data.total" @size-change="getList()" @current-change="getList()" />

        <!-- 抽屉/弹框 -->
        <el-dialog v-model="dialogData.dialogVisible" :close-on-click-modal="false" width="3.6rem"
            :title="dialogData.dialogTitle" @close="closeDialog">
            <el-form :model="dialogData.form" :rules="dialogData.formRules" hide-required-asterisk ref="refForm" label-postion="left" label-width="0.8rem">
                <el-form-item label="供应商" prop="name">
                    <el-input clearable style="width: 2.2rem;" v-model="dialogData.form.name" placeholder="请输入供应商" />
                </el-form-item>
                <el-form-item label="联系人" prop="contact_person">
                    <el-input clearable style="width: 2.2rem;" v-model="dialogData.form.contact_person" placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="联系电话" prop="contact_phone">
                    <el-input clearable style="width: 2.2rem;" v-model="dialogData.form.contact_phone" placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input clearable style="width: 2.2rem;" v-model="dialogData.form.address" placeholder="请输入地址" />
                </el-form-item>
                <el-form-item label="电子邮箱" prop="contact_email">
                    <el-input clearable style="width: 2.2rem;" v-model="dialogData.form.contact_email" placeholder="请输入电子邮箱" />
                </el-form-item>
                <el-form-item label="合作日期" prop="date">
                    <el-date-picker
                        style="width: 2.2rem;"
                        v-model="dialogData.form.date"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="YYYY-MM-DD"
                    />
                </el-form-item>           
            </el-form>
            <template #footer>
                <span>
                    <el-button @click="closeDialog">取消</el-button>
                    <el-button type="primary" @click="submit">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<script setup>
import { ref, onMounted, reactive, watchEffect } from 'vue'
import http from '@/api/home'
import utils from '@/utils/utils'

const refForm = ref()

// 数据集
const data = reactive({
    // 搜索表单对象
    searchform: {},
    // 表格数据
    tableList: [],
    // 表格高度
    tableHeight: 0,
    // 表格加载
    tableLoading: false,
    // 分页 
    pageNum: 1,
    pageSize: 20,
    total: 0,
})

// 弹框
const dialogData = reactive({
    // 是否显示
    dialogVisible: false,
    // 标题
    dialogTitle: '',
    // 表单数据 
    form: {},
    // 校验规则
    formRules: {
        name: [{ required: true, message: '请输入供应商', trigger: 'blur' }],
        contact_person: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        contact_phone: [{ required: true, message: '请选择联系电话', trigger: 'blur' }],
        address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        date: [{ required: true, message: '请选择日期', trigger: 'change' }],
    },
})

onMounted(() => {
    getList()
})

// 获取列表
const getList = (reset = false) => {
    if (reset) data.pageNum = 1
    data.tableLoading = true
    http.supplierList({
        ...data.searchform,
        pageNum: data.pageNum,
        pageSize: data.pageSize
    }).then(res => {
        data.tableLoading = false
        data.tableList = res.data.records
        data.total = res.data.total
    }).finally(() => {
        data.tableLoading = false
    })
}

// 打开对话框
const openDialog = (row, title) => {
    dialogData.dialogVisible = true
    if (row) {
        dialogData.form = { ...row }
        dialogData.form.date = [utils.formatDate(new Date(row.start_time).getTime(), 'Y-M-D'), utils.formatDate(new Date(row.end_time).getTime(), 'Y-M-D')]
    } else {
        dialogData.form = {}
    }
    dialogData.dialogTitle = title
}

// 关闭对话框
const closeDialog = () => {
    dialogData.dialogVisible = false; 
    refForm.value.resetFields(); 
    dialogData.form = {}
}

//提交
const submit = () => {
    refForm.value.validate((valid) => {
        if (valid) {        
            // 日期处理
            if (dialogData.form.date) {
                dialogData.form.start_time = dialogData.form.date[0] + ' 00:00:00'
                dialogData.form.end_time = dialogData.form.date[1] + ' 23:59:59'
            }
            if (dialogData.dialogTitle == '添加') {                
                http.supplierAdd(dialogData.form).then(res => {
                    ElMessage({ type: 'success', message: res.message })
                    dialogData.dialogVisible = false
                    getList()
                })
            } else {
                http.supplierUpdate(dialogData.form).then(res => {
                    ElMessage({ type: 'success', message: res.message })
                    dialogData.dialogVisible = false
                    getList()
                })
            }
        }
    })
}

// 删除
const deleteSupplier = (data) => {
    ElMessageBox.confirm(
        '是否删除该供应商?',
        '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        http.supplierDelete({ id: data.id }).then(res => {
            ElMessage({ type: 'success', message: res.message })
            getList()
        })
    })
}

// 设置表格高度
const setTableHeight = () => {
    let tableH = 320 // 距离下方的距离 
    let tableHeightDetail = window.innerHeight - tableH
    if (tableHeightDetail <= 400) {
        data.tableHeight = 400
    } else {
        data.tableHeight = window.innerHeight - tableH 
    }
}
watchEffect(() => setTableHeight())

</script>

<style lang="scss" scoped>
.card {
    box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
    margin-top: 0.14rem;
    height: calc(100vh - 0.68rem - 0.7rem);

    .title {
        font-size: 0.2rem;
        font-weight: bold;
        color: #000;
        padding-bottom: 0.1rem;
        border-bottom: 4px solid $ncpColor;
    }

    .role-input {
        width: 2rem;
        margin-right: 0.2rem;
    }

    .table {
        width: 100%;
        .table-sale {
            color: $helpColor;
        }
        .table-info {
            color: #9CA3AF;
            font-size: 0.12rem;
        }   
    }

    .table-edit {
        color: $otherColor;
        margin-right: 0.2rem;
        cursor: pointer;

        &:hover {
            text-decoration: underline;
        }
    }

    .disabled {
        pointer-events: none;
        opacity: 0.8;
    }

    .table-delete {
        color: $helpColor;
        cursor: pointer;

        &:hover {
            text-decoration: underline;
        }
    }

    :deep(.el-drawer__title) {
        font-size: 0.16rem;
    }
    .content {
        position: relative;
        height: 100%;
        .upload-img .img {
            width: 1rem;
            height: 1rem;
            display: block;
        }
        :deep(.upload-img .el-upload) {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
            &:hover {
                border-color: var(--el-color-primary);
            }
        }
        .el-icon.img-uploader-icon {
            font-size: 0.28rem;
            color: #8c939d;
            width: 1rem;
            height: 1rem;
            text-align: center;
        }

        .footer {
            position: absolute;
            bottom: 0;
            left: 0;
        }
    }

}
</style>